package com.yswee.framework.widget.loading;

import java.util.ArrayList;

import com.yswee.framework.Application;
import com.yswee.framework.R;
import com.yswee.framework.data.BaseArrayData;
import com.yswee.framework.model.BaseListModel;
import com.yswee.framework.model.PageType;
import com.yswee.framework.net.http.Entity;
import com.yswee.framework.widget.adapterview.withmodel.MGridView;
import com.yswee.framework.widget.adapterview.withmodel.MListView;
import com.yswee.framework.widget.layoutview.ILayoutView;
import com.yswee.framework.widget.pulltorefresh.PullToRefreshBase;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Toast;


public abstract class LoadingPullToRefreshGridView<T> extends LoadingLayout {

	protected PullToRefreshGridView mListView;
	
	public LoadingPullToRefreshGridView(Context context) {
		super(context);
	}
	
	public LoadingPullToRefreshGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected View getLayoutView() {
		mListView = new PullToRefreshGridView(getContext());
		return mListView;
	}

	@Override
	protected int getLayoutResId() {
		return 0;
	}
	
	@Override
	public void onApplyLoadingData() {
		mListView.getRefreshableView().getFirstPage();
	}

	protected BaseListModel<T> getMode() {
		return mListView.getRefreshableView().getModel();
	}

	protected abstract BaseListModel<T> createMode();
	
	protected void initializeGridView(MGridView<T> listview) {
		
	}
	
	/**
	 * 是否允许加载下一页
	 * @return
	 */
	protected boolean enabledLoadNextPage() {
		return true;
	}
	
	/**
	 * 加载失败的提示语
	 * @return
	 */
	protected int getLoadErrResId() {
		return R.string.get_data_err;
	}
	
	/**
	 * ListView的加载方式
	 * LISTVIEW_TYPE_VIEW  (必须override createLayoutView 方法）
	 * LISTVIEW_TYPE_DATABINDING （必须override getLayoutResId/getLayoutView 方法）
	 * @return
	 */
	protected int getListViewType() {
		return MListView.LISTVIEW_TYPE_VIEW;
	}
	
	protected int getViewTypeCount() {
		return 1;
	}

	protected int getLayoutResId(int position, T t, int itemViewType) {
		return 0;
	}
	
	protected ILayoutView<T> getLayoutView(int position, T t, int itemViewType) {
		return null;
	}
	
	/**
	 * 下拉刷新的方式
	 * @return MODE_PULL_DOWN_TO_REFRESH 下拉刷新， MODE_PULL_UP_TO_REFRESH 上拉刷新
	 */
	protected int getPullMode() {
		return PullToRefreshBase.MODE_PULL_DOWN_TO_REFRESH;
	}

	/**
	 * 加载下一页的方式
	 * @return MODE_LOADMORE_BOTTOM 触底加载， MODE_LOADMORE_TOP 触顶加载
	 */
	protected int getLoadMoreMode() {
		return MListView.MODE_LOADMORE_BOTTOM;
	}
	
	/**
	 * 请求第一页数据返回后是否滑动至顶部或底部
	 * @return
	 */
	protected boolean scrollToEdgeOnGetFirstPage() {
		return true;
	}
	
	/**
	 * 请求缓存数据返回后是否滑动至顶部或底部
	 * @return
	 */
	protected boolean scrollToEdgeOnGetCacheData() {
		return false;
	}
	
	/**
	 * 是否准备好加载数据
	 * @param isFirstPage
	 * @return
	 */
	protected boolean isReadyLoadData(boolean isFirstPage) {
		return true;
	}
	
	/**
	 * 转换数据源
	 * @param array
	 * @return
	 */
	protected ArrayList<T> transferDataSource(ArrayList<T> array) {
		return array;
	}
	
	/**
	 * 获取第一页数据
	 * @param loadCache
	 */
	protected void onGetFirstPage(boolean loadCache) {
		
	}
	
	/**
	 * 获取数据源
	 * @return
	 */
	public ArrayList<T> getDataSource() {
		if(mListView != null) {
			return mListView.getRefreshableView().getDataSource();
		}
		return null;
	}
	
	/**
	 * 设置数据源
	 * @param data
	 */
	public void setDataSource(ArrayList<T> data) {
		if(mListView != null) {
			mListView.getRefreshableView().setDataSource(data);
		}
	}
	
	/**
	 * 刷新
	 */
	public void refresh() {
		mListView.getRefreshableView().getFirstPage();
	}
	
	/**
	 * 通知刷新
	 */
	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		if (mListView != null && mListView.getRefreshableView() != null) {
			mListView.getRefreshableView().notifyDataSetChanged();
		}
	}
	
	/**
	 * 增加数据
	 * @param t
	 * @return
	 */
	public boolean addData(T t) {
		return mListView.addData(t);
	}
	
	/**
	 * 增加数据
	 * @param index 索引
	 * @param t 数据
	 * @return
	 */
	public boolean addData(int index, T t) {
		return mListView.addData(index, t);
	}
	
	/**
	 * 移除数据
	 * @param t
	 * @return
	 */
	public boolean removeData(T t) {
		return mListView.removeData(t);
	}
	
	/**
	 * 移除数据
	 * @param index 索引
	 * @return
	 */
	public T removeData(int index) {
		return mListView.removeData(index);
	}
	
	/**
	 * 清空数据
	 * @return
	 */
	public boolean clearData() {
		return mListView.clearData();
	}
	
	public class PullToRefreshGridView extends com.yswee.framework.widget.pulltorefresh.adapterview.withmodel.PullToRefreshGridView<T> {

		
		public PullToRefreshGridView(Context context) {
			super(context);
		}
		
		@Override
		protected int getPullMode() {
			return LoadingPullToRefreshGridView.this.getPullMode();
		}

		@Override
		protected MGridView<T> createMGridView(Context context,
				AttributeSet attrs) {
			return new InternalGridView(context, attrs);
		}
		
		public class InternalGridView extends com.yswee.framework.widget.pulltorefresh.adapterview.withmodel.PullToRefreshGridView<T>.InternalGridView {
			
			public InternalGridView(Context context, AttributeSet attrs) {
				super(context, attrs);
			}
			
			@Override
			public boolean isReadyLoadData(boolean isFirstPage) {
				return LoadingPullToRefreshGridView.this.isReadyLoadData(isFirstPage);
			}
			
			@Override
			protected int getLoadMoreMode() {
				return LoadingPullToRefreshGridView.this.getLoadMoreMode();
			}
			
			@Override
			protected boolean enabledLoadNextPage() {
				return LoadingPullToRefreshGridView.this.enabledLoadNextPage();
			}

			@Override
			protected int getListViewType() {
				return LoadingPullToRefreshGridView.this.getListViewType();
			}
			
			@Override
			protected int getViewTypeCount() {
				return LoadingPullToRefreshGridView.this.getViewTypeCount();
			}

			@Override
			protected int getLayoutResId(int position, T t, int itemViewType) {
				return LoadingPullToRefreshGridView.this.getLayoutResId(position, t, itemViewType);
			}

			@Override
			protected ILayoutView<T> getLayoutView(int position, T t, int itemViewType) {
				return LoadingPullToRefreshGridView.this.getLayoutView(position, t, itemViewType);
			}

			@Override
			protected void initializeGridView() {
				super.initializeGridView();
				setHorizontalScrollBarEnabled(false);
				setVerticalScrollBarEnabled(true);
				hideFadingEdge();
				LoadingPullToRefreshGridView.this.initializeGridView(this);
			}

			@Override
			protected BaseListModel<T> createMode() {
				return LoadingPullToRefreshGridView.this.createMode();
			}
			
			@Override
			protected boolean scrollToEdgeOnGetCacheData() {
				return LoadingPullToRefreshGridView.this.scrollToEdgeOnGetCacheData();
			}
			
			@Override
			protected boolean scrollToEdgeOnGetFirstPage() {
				return LoadingPullToRefreshGridView.this.scrollToEdgeOnGetFirstPage();
			}
			
			@Override
			protected void onBeginLoad(boolean isFirstPage) {
				super.onBeginLoad(isFirstPage);
				if(isFirstPage && getAdapterCount() < 1) {
					gotoLoading();
				}
			}
			
			@Override
			public void getFirstPage(boolean loadCache) {
				super.getFirstPage(loadCache);
				LoadingPullToRefreshGridView.this.onGetFirstPage(loadCache);
			}
			
			@Override
			protected void onLoadSucc(Entity<BaseArrayData<T>> entity, PageType pagetype) {
				super.onLoadSucc(entity, pagetype);
				gotoSuccessful();
			}
			
			@Override
			protected void onLoadBlank(Entity<BaseArrayData<T>> entity, PageType pagetype) {
				superOnLoadBank(entity, pagetype);
				if(pagetype == PageType.FirstPage && getAdapterCount() < 1)
					gotoBlank();
			}
			
			protected void superOnLoadBank(Entity<BaseArrayData<T>> entity, PageType pagetype) {
				super.onLoadBlank(entity, pagetype);
			}
			
			@Override
			protected void onLoadError(Entity<BaseArrayData<T>> entity, PageType pagetype) {
				superOnLoadError(entity, pagetype);
				if(pagetype == PageType.FirstPage && getAdapterCount() < 1) {
					gotoError();
				} else {
					if(entity.getEntityStatus() != Entity.NETINVALID) {
						int resid = LoadingPullToRefreshGridView.this.getLoadErrResId();
						if(resid > 0) {
							Toast.makeText(Application.get(), resid, Toast.LENGTH_SHORT).show();
						}
					}
				}
			}
			
			protected void superOnLoadError(Entity<BaseArrayData<T>> entity, PageType pagetype) {
				super.onLoadError(entity, pagetype);
			}
			
			@Override
			public void setDataSource(ArrayList<T> array) {
				super.setDataSource(transferDataSource(array));
			}
			
		}
	}
}
